<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
       <el-col :span="12">
            <el-form-item label="工单号" prop="workOrderNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.workOrderNumber" placeholder="请填写工单号"    disabled="" ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="设备编码" prop="equipmentCode"
                :rules="[
                 ]">
              <el-input v-model="inputForm.equipmentCode" placeholder="请填写设备编码"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="厂" prop="factory"
                :rules="[
                 ]">
              <el-input v-model="inputForm.factory" placeholder="请填写厂"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="区域" prop="area"
                :rules="[
                 ]">
              <el-input v-model="inputForm.area" placeholder="请填写区域"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="主体设备" prop="mainEquipment"
                :rules="[
                 ]">
              <el-input v-model="inputForm.mainEquipment" placeholder="请填写主体设备"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="任务来源" prop="taskSource"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.taskSource" placeholder="请填写任务来源"     disabled=""></el-input> -->
               <el-select v-model="inputForm.taskSource" placeholder="请填写任务来源" style="width:100%;" disabled="">
                 <el-option v-for="taskSource in $dictUtils.getDictList('repair_source')" :label="taskSource.label" :value="taskSource.value" :key="taskSource.value" ></el-option>
               </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="施工内容" prop="roadworkContent"
                :rules="[
                 ]">
              <el-input v-model="inputForm.roadworkContent" placeholder="请填写施工内容"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="接办人" prop="unsuccessfulPeople"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.unsuccessfulPeople" placeholder="请填写接办人"     disabled=""></el-input> -->
              <user-select :limit='1' :value="inputForm.unsuccessfulPeople" @getValue='(value) => {inputForm.unsuccessfulPeople=value}' @getUserName='getUserName(arguments)' disabled=""></user-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="工号" prop="workNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.workNumber" placeholder="请填写工号"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
                    <el-form-item label="检修标准工时" prop="overhaulPeople"
                        :rules="[
                         ]">
                      <el-input v-model="inputForm.overhaulPeople" placeholder="请填写检修标准工时" disabled=""></el-input>
                   </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="检修标准项目承办人数" prop="repirpeople"
                        :rules="[
                         ]">
                      <el-input v-model="inputForm.repirpeople" placeholder="请填写检修标准项目承办人数" disabled=""></el-input>
                   </el-form-item>
                </el-col>
        <el-col :span="12">
            <el-form-item label="完成时间" prop="completeTime"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.completeTime" placeholder="请填写完成时间"   disabled="" ></el-input> -->
              <el-date-picker
                    style="width:100%;"
                    v-model="inputForm.completeTime"
                    type="datetime"
                    placeholder="选择日期时间"
                    default-time="12:00:00" disabled="">
              </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="工时" prop="workTime"
                :rules="[
                 ]">
              <el-input v-model="inputForm.workTime" placeholder="请填写工时"    disabled="" ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="状态" prop="state"
                :rules="[
                 ]">
              <el-select v-model="inputForm.state" placeholder="请填状态" style="width:100%;" disabled="">
              <el-option v-for="state in $dictUtils.getDictList('repair_statement')" :label="state.label" :value="state.value" :key="state.value"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="项目协办人" prop="projectMan"
                :rules="[
                 ]">
              <el-input v-model="inputForm.projectMan" placeholder="请填写项目协办人"     disabled=""></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="24">
            <el-form-item label="项目协办人" prop="projectMan"
                :rules="[
                 ]">
              <user-select :limit='10' :value="inputForm.projectMan" @getValue='(value) => {inputForm.projectMan=value}' @getUserName='getUserName(arguments)' ></user-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="点检意见" prop="tallyOpinion"
                :rules="[
                 ]" v-if="tally">
              <!-- <el-input v-model="inputForm.tallyOpinion" placeholder="请填写点检意见"   ></el-input> -->
              <el-select v-model="inputForm.tallyOpinion" placeholder="请选择"  style="width: 100%;">
                        <el-option
                          v-for="item in $dictUtils.getDictList('repair_opinion')"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                        </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="检修意见" prop="maintenanceOpinion"
                :rules="[
                 ]" v-if="maintenance">
              <el-select v-model="inputForm.maintenanceOpinion" placeholder="请选择"  style="width: 100%;">
                        <el-option
                          v-for="item in $dictUtils.getDictList('repair_opinion')"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value">
                        </el-option>
                        </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="生产意见" prop="productionOpinion"
                :rules="[
                 ]" v-if="production">
             <el-select v-model="inputForm.productionOpinion" placeholder="请选择"  style="width: 100%;">
                       <el-option
                         v-for="item in $dictUtils.getDictList('repair_opinion')"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
                       </el-option>
                       </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item v-if="check" label="点检意见说明" prop="remarks"
                :rules="[
                 ]" >
              <el-input  type="textarea" v-model="inputForm.remarks" placeholder="请填写意见说明"  ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item v-if="repair" label="检修意见说明" prop="checkRemark"
                :rules="[
                 ]" >
              <el-input type="textarea" v-model="inputForm.checkRemark" placeholder="请填写意见说明"  ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item v-if="product" label="生产意见说明" prop="productRemark"
                :rules="[
                 ]" >
              <el-input  type="textarea" v-model="inputForm.productRemark" placeholder="请填写意见说明"  ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :disabled="method === 'changestate'" @click="visible = false">关闭</el-button>
      <el-button :disabled="method === 'changestate'" type="primary" v-if="method != 'view'" @click="open()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import UserSelect from '@/components/userSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          workOrderNumber: '',
          equipmentCode: '',
          factory: '',
          area: '',
          mainEquipment: '',
          taskSource: '',
          roadworkContent: '',
          unsuccessfulPeople: '',
          workNumber: '',
          completeTime: '',
          tallyOpinion: '',
          maintenanceOpinion: '',
          productionOpinion: '',
          workTime: '',
          state: '',
          projectMan: '',
          remarks: '',
          testId: '',
          overhaulPeople: '',
          repirpeople: '',
          checkRemark: '',
          productRemark: '',
          processId: ''
        },
        loginName: '',
        tally: true,
        maintenance: true,
        production: true,
        check: false,
        repair: false,
        product: false,
        opinionValue: ''
      }
    },
    watch: {
      'this.loginName': function (newVal, oldVal) {
        if (newVal === '点检员') {
          this.repair = false
          this.product = false
      } else if (newVal === '检修员') {
          this.product = false
          this.check = false
      } else if (newVal === '生产岗位人员') {
          this.check = false
          this.repair = false
      } else {
          this.check = false
          this.repair = false
          this.product = false
      }
      },
      'inputForm.tallyOpinion': function (newVal, oldVal) {
        console.log(newVal)
        console.log(oldVal)
        this.opinionValue = newVal
        if (this.opinionValue === '1') {
          this.check = false
        } else if (this.opinionValue === '2') {
           this.check = true
          }
      },
      'inputForm.maintenanceOpinion': function (newVal, oldVal) {
        this.opinionValue = newVal
        if (this.opinionValue === '1') {
          this.repair = false
        } else if (this.opinionValue === '2') {
          this.repair = true
          }
      },
      'inputForm.productionOpinion': function (newVal, oldVal) {
        this.opinionValue = newVal
        if (this.opinionValue === '1') {
          this.product = false
        } else if (this.opinionValue === '2') {
          this.product = true
          }
      }
    },
    components: {
      UserSelect
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        this.$http({
           url: `/sys/log/logId`,
           method: 'get'
         }).then(({data}) => {
          this.loginName = data.logId.roleNames
          // if (this.loginName !== '点检员' && this.loginName !== '生产岗位人员' && this.loginName !== '检修员' && method !== 'view') {
          // this.$alert('您没有此权限', '提示', {
          //   confirmButtonText: '确定',
          //   type: 'warning'
          // })
          // } else {
            // if (newVal === '点检员') {
            //     this.repair = false
            //     this.product = false
            // } else if (newVal === '检修员') {
            //     this.product = false
            //     this.check = false
            // } else if (newVal === '生产岗位人员') {
            //     this.check = false
            //     this.repair = false
            // } else {
            //     this.check = false
            //     this.repair = false
            //     this.product = false
            // }
          if (this.loginName === '点检员') {
            this.tally = true
            this.maintenance = false
            this.production = false
            this.repair = false
            this.product = false
          } else if (this.loginName === '检修员') {
            this.tally = false
            this.maintenance = true
            this.production = false
            this.product = false
            this.check = false
          } else if (this.loginName === '生产岗位人员') {
            this.tally = false
            this.maintenance = false
            this.production = true
            this.check = false
             this.repair = false
          } else {
            this.tally = false
            this.maintenance = false
            this.production = false
            this.check = false
            this.repair = false
            this.product = false
          }
          if (method === 'add') {
            this.title = `新建检修项目执行`
          } else if (method === 'edit') {
            this.title = '修改检修项目执行'
          } else if (method === 'view') {
            this.title = '查看检修项目执行'
          }
          this.visible = true
          this.loading = false
          this.$nextTick(() => {
            this.$refs.inputForm.resetFields()
            if (method === 'edit' || method === 'view' || method === 'changestate') { // 修改或者查看
              this.loading = true
              this.$http({
                url: `/flowform/form/repairAcceptance/queryById?id=${this.inputForm.id}`,
                method: 'get'
              }).then(({data}) => {
                this.inputForm = this.recover(this.inputForm, data.repairAcceptance)
                if (method === 'changestate') {
                  this.inputForm.state = 1
                  this.doSubmit()
                }
                this.loading = false
              })
            }
          })
          // }
          })
      },
      // 打开提示
     open () {
       if (this.opinionValue === "1") {
         this.opinionValue = "同意"
        } if (this.opinionValue === "2") {
         this.opinionValue = "不同意"
        }
            this.$confirm("您当前选择" + this.opinionValue + ", 是否继续提交?", '提示', {
              confirmButtonText: '提交',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.doSubmit()
              this.$message({
                type: 'success',
                message: '提交成功!'
              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消提交'
              })
            })
          },
      // 表单提交
      doSubmit () {
        this.$http({
          url: `/flowform/process/repairTaskDistributionProcess/flowableupdata`,
          method: 'get',
          params: {
            testId: this.inputForm.testId,
            key: 'tally_opinion',
            value: this.inputForm.tallyOpinion
          }
        })
        this.$http({
          url: `/flowform/process/repairTaskDistributionProcess/flowableupdata`,
          method: 'get',
          params: {
            testId: this.inputForm.testId,
            key: 'remark',
            value: this.inputForm.remarks
          }
        })
        this.$http({
          url: `/flowform/process/repairTaskDistributionProcess/flowableupdata`,
          method: 'get',
          params: {
            testId: this.inputForm.testId,
            key: 'maintenance_opinion',
            value: this.inputForm.maintenanceOpinion
          }
        })
        this.$http({
          url: `/flowform/process/repairTaskDistributionProcess/flowableupdata`,
          method: 'get',
          params: {
            testId: this.inputForm.testId,
            key: 'production_opinion',
            value: this.inputForm.productionOpinion
          }
        })
        this.$http({
          url: `/flowform/process/repairTaskDistributionProcess/flowableupdata`,
          method: 'get',
          params: {
            testId: this.inputForm.testId,
            key: 'remark',
            value: this.inputForm.remarks
          }
        })
        this.$http({
          url: `/flowform/process/repairTaskDistributionProcess/flowableupdata`,
          method: 'get',
          params: {
            testId: this.inputForm.testId,
            key: 'check_remark',
            value: this.inputForm.checkRemark
          }
        })
        this.$http({
          url: `/flowform/process/repairTaskDistributionProcess/flowableupdata`,
          method: 'get',
          params: {
            testId: this.inputForm.testId,
            key: 'product_remark',
            value: this.inputForm.productRemark
          }
        })
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/flowform/form/repairAcceptance/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
